package com.shop.service.impl;

import com.shop.dto.CarItem;
import com.shop.mapper.CartMapper;


import com.shop.model.Cart;
import com.shop.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lenovo
 * @version 1.0
 * @date 2025/4/29 11:36
 * @className CardServiceImpl
 */
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;

    @Override
    public List<Cart> getCartsByUserId(Long userId) {
        return cartMapper.selectCartsByUserId(userId);
    }
    @Override
    public Map<String, Object> decreaseQuantity(Long goodId) {
        Map<String, Object> result = new HashMap<>();
        int count = cartMapper.getCountByGoodId(goodId);
        if (count > 1) {
            cartMapper.decreaseQuantity(goodId);
            Cart cart = cartMapper.getCartByGoodId(goodId);
            result.put("success", true);
            result.put("removed", false);
            result.put("quantity", cart.getCount());
            result.put("price", cart.getPrice());
        } else if (count == 1) {
            cartMapper.deleteCartByGoodId(goodId);
            result.put("success", true);
            result.put("removed", true);
        } else {
            result.put("success", false);
        }
        return result;
    }

    @Override
    public Map<String, Object> increaseQuantity(Long goodId) {
        Map<String, Object> result = new HashMap<>();
        int stock = cartMapper.getStockByGoodId(goodId);
        int currentCount = cartMapper.getCountByGoodId(goodId);

        if (currentCount + 1 <= stock) {
            cartMapper.increaseQuantity(goodId);
            Cart cart = cartMapper.getCartByGoodId(goodId);
            result.put("success", true);
            result.put("quantity", cart.getCount());
            result.put("price", cart.getPrice());
        } else {
            result.put("success", false);
            result.put("message", "该商品库存不足，无法增加数量");
        }

        return result;
    }
    @Override
    public Map<String, Object> deleteCartItem(Long goodId) {
        Map<String, Object> result = new HashMap<>();
        try {
            int rowsAffected = cartMapper.deleteCartByGoodId(goodId);
            if (rowsAffected > 0) {
                result.put("success", true);
            } else {
                result.put("success", false);
            }
        } catch (Exception e) {
            result.put("success", false);
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public Map<String, Object> checkout(Long goodId, Long cartId, int count) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 1. 检查库存
            int stock = cartMapper.getStockByGoodId(goodId);
            if (stock < count) {
                result.put("success", false);
                result.put("message", "库存不足");
                return result;
            }

            // 2. 更新库存
            cartMapper.updateStock(goodId, count);

            // 3. 删除购物车项
            cartMapper.deleteCartById(cartId);

            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "结算过程中发生错误");
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int addCartItem(List<CarItem> carItemList) {
        if (carItemList == null || carItemList.size() == 0) {
            return 0;
        }
        for (CarItem carItem : carItemList) {
            int a=cartMapper.addCartItem(carItem);
            if (a>0){
                return 1;
            }else {
                return 0;
            }
        }
        return 1;
    }

    @Override
    public void clearCart(Long userId) {
        cartMapper.deleteAllByUserId(userId);
    }

    @Override
    public List<CarItem> getCartItemsByUserId(Long id) {
        return cartMapper.selectCartItemsByUserId(id);
    }

    @Override
    public void addCart(Cart cart) {
        cartMapper.addCart(cart);
    }

    @Override
    public Cart getCartByUserIdAndGoodId(Long userId, Long goodId) {
        return cartMapper.getCartByUserIdAndGoodId(userId, goodId);
    }

    @Override
    public void updateCart(Cart cart) {
        cartMapper.updateCart(cart);
    }

    @Override
    public Cart getTotalPrice(Integer id) {
        return cartMapper.getTotalPrice(id);
    }

    @Override
    public CarItem getCartItem(Integer id) {
        CarItem carItem=cartMapper.getCartItem(id);
        return carItem;
    }

    @Override
    public List<CarItem> getCar(Long id) {
        return null;
    }

    @Override
    public int deleteCartbyId(Integer id) {
        int a=cartMapper.deleteCartbyId(id);
        return a;
    }
}
